package org.jeecg.modules.amazon.amazonproduct.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.modules.amazon.amazonproduct.entity.AmazonTaskProduct;
import org.jeecg.modules.amazon.amazonproduct.service.IAmazonTaskProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 亚马逊商品表
 * @Author: jeecg-boot
 * @Date:   2025-11-11
 * @Version: V1.0
 */
@Tag(name="亚马逊商品表")
@RestController
@RequestMapping("/amazonproduct/amazonTaskProduct")
@Slf4j
public class AmazonTaskProductController extends JeecgController<AmazonTaskProduct, IAmazonTaskProductService> {
	@Autowired
	private IAmazonTaskProductService amazonTaskProductService;

	/**
	 * 分页列表查询
	 *
	 * @param amazonTaskProduct
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "亚马逊商品表-分页列表查询")
	@Operation(summary="亚马逊商品表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<AmazonTaskProduct>> queryPageList(AmazonTaskProduct amazonTaskProduct,
														  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
														  HttpServletRequest req) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String id = loginUser.getId();
		String username = loginUser.getUsername();



		QueryWrapper<AmazonTaskProduct> queryWrapper = QueryGenerator.initQueryWrapper(amazonTaskProduct, req.getParameterMap());
		if (StrUtil.isNotEmpty(username) && !username.equals("admin")){
			queryWrapper.eq("user_id",id);
		}
		queryWrapper.eq("is_parent","1");
		Page<AmazonTaskProduct> page = new Page<AmazonTaskProduct>(pageNo, pageSize);
		IPage<AmazonTaskProduct> pageList = amazonTaskProductService.page(page, queryWrapper);
//		for (AmazonTaskProduct record : pageList.getRecords()) {
//			String productImage = record.getProductImage();
//			JSONArray objects = JSONObject.parseArray(productImage);
//			System.out.println(objects);
//		}
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param amazonTaskProduct
	 * @return
	 */
	@AutoLog(value = "亚马逊商品表-添加")
	@Operation(summary="亚马逊商品表-添加")
	@RequiresPermissions("amazonproduct:amazon_task_product:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody AmazonTaskProduct amazonTaskProduct) {
		amazonTaskProductService.save(amazonTaskProduct);

		return Result.OK("添加成功！");
	}

	/**
	 *   http采集工具添加
	 *
	 * @param jsonObject
	 * @return
	 */
	@AutoLog(value = "http采集工具添加亚马逊商品表")
	@Operation(summary="http采集工具亚马逊商品表-添加")
	@IgnoreAuth
	@PostMapping(value = "/httpadd")
	public Result<String> HttpAdd(@RequestBody JSONObject jsonObject) {
		amazonTaskProductService.HttpAdd(jsonObject);
		return new Result(1000,"添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param amazonTaskProduct
	 * @return
	 */
	@AutoLog(value = "亚马逊商品表-编辑")
	@Operation(summary="亚马逊商品表-编辑")
	@RequiresPermissions("amazonproduct:amazon_task_product:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody AmazonTaskProduct amazonTaskProduct) {
		amazonTaskProductService.updateById(amazonTaskProduct);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "亚马逊商品表-通过id删除")
	@Operation(summary="亚马逊商品表-通过id删除")
	@RequiresPermissions("amazonproduct:amazon_task_product:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		amazonTaskProductService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "亚马逊商品表-批量删除")
	@Operation(summary="亚马逊商品表-批量删除")
	@RequiresPermissions("amazonproduct:amazon_task_product:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.amazonTaskProductService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "亚马逊商品表-通过id查询")
	@Operation(summary="亚马逊商品表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<AmazonTaskProduct> queryById(@RequestParam(name="id",required=true) String id) {
		AmazonTaskProduct amazonTaskProduct = amazonTaskProductService.getById(id);
		if(amazonTaskProduct==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(amazonTaskProduct);
	}


	/**
	 * 通过id查询sku
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "亚马逊商品表-通过id查询")
	@Operation(summary="亚马逊商品表-通过id查询sku")
	@GetMapping(value = "/querySkuById")
	public Result<List<AmazonTaskProduct>> querySkuById(@RequestParam(name="id",required=true) String id) {
		AmazonTaskProduct amazonTaskProduct = amazonTaskProductService.getById(id);
		if(amazonTaskProduct==null) {
			return Result.error("未找到对应数据");
		}
	   List<AmazonTaskProduct> amazonTaskProductList= amazonTaskProductService.querySkuByparentSku(amazonTaskProduct.getSku());
		return Result.OK(amazonTaskProductList);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param amazonTaskProduct
	 */
	@RequiresPermissions("amazonproduct:amazon_task_product:exportXls")
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, AmazonTaskProduct amazonTaskProduct) {
		return super.exportXls(request, amazonTaskProduct, AmazonTaskProduct.class, "亚马逊商品表");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequiresPermissions("amazonproduct:amazon_task_product:importExcel")
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, AmazonTaskProduct.class);
	}

}
